<template>
  <!-- role信息管理页 -->
  <div class="main">
    <!-- 分类信息列表渲染 -->
    <el-table
      :data="roles"
      style="width: 1002px; margin: 0 auto"
      max-height="352"
      stripe
      border
      :header-cell-style="headerStyle"
    >
      <!-- ID项 -->
      <el-table-column prop="roleId" label="ID" width="200"> </el-table-column>
      <!-- 名称项 -->
      <el-table-column
        prop="roleName"
        label="名称"
        width="200"
      ></el-table-column>
      <!-- 描述 -->
      <el-table-column
        prop="roleDes"
        label="角色描述"
        width="200"
      ></el-table-column>
      <!-- 操作栏 -->
      <el-table-column label="操作" width="400">
        <template slot-scope="scope">
          <!-- 编辑按钮 -->
          <el-button
            :disabled="roles[scope.$index].roleId == 1"
            type="primary"
            @click="editrole(scope.row)"
            icon="el-icon-edit"
            size="small"
          ></el-button>
          <!-- 删除按钮 -->
          <el-button
            :disabled="roles[scope.$index].roleId == 1"
            @click="deleteRow(scope.$index, roles)"
            size="small"
            type="primary"
            icon="el-icon-delete"
          >
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加分类按钮 -->
    <el-button
      @click="addDialogFlag = true"
      class="addButton"
      size="medium"
      type="primary"
      round
      style="margin-bottom: 10px"
      >添加角色</el-button
    >
    <!-- 添加提示输入名称dialog -->
    <el-dialog
      title="请输入必要信息："
      :visible.sync="addDialogFlag"
      width="40%"
      center
      top="20vh"
    >
      <span class="title">角色名：</span
      ><el-input v-model="addRole.roleName" size="small"> </el-input>
      <span class="title">角色描述：</span
      ><el-input v-model="addRole.roleDes" size="small"> </el-input>
      <span class="title">权限选择：</span>
      <el-cascader
        style="width: 100%"
        v-model="addRole.powerList"
        :options="menus"
        :props="props"
        collapse-tags
        clearable
        size="small"
      ></el-cascader>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelAdd">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑dialog -->
    <el-dialog
      title="请编辑必要信息："
      :visible.sync="editDialogFlag"
      width="40%"
      center
      top="20vh"
    >
      <span class="title">角色名：</span>
      <el-input v-model="editRole.roleName" size="small"> </el-input>
      <span class="title">角色描述：</span>
      <el-input v-model="editRole.roleDes" size="small"> </el-input>
      <span class="title">权限选择：</span>
      <el-cascader
        style="width: 100%"
        v-model="editRole.powerListOptions"
        :options="menus"
        :props="props"
        collapse-tags
        size="small"
      ></el-cascader>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelEdit()">取 消</el-button>
        <el-button type="primary" @click="edit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      props: { multiple: true },
      roles: [],
      addRole: {},
      editRole: {},
      menus: [],
      addDialogFlag: false,
      editDialogFlag: false,
    };
  },
  mounted() {
    this.getRoles();
    this.getPowerList();
  },
  methods: {
    headerStyle() {
      return "color: #000;background-color: #c5464a;height:60px;";
    },
    getPowerList() {
      this.$http.get("aside-menu/list").then((response) => {
        this.menus = response.data.result;
      });
    },
    getRoles() {
      this.$http.get("role/list").then((response) => {
        this.roles = response.data.result;
        this.roles.forEach((element) => {
          element.powerListOptions = JSON.parse(element.powerListOptions);
        });
      });
    },
    deleteRow(index, data) {
      const id = data[index].roleId;
      const name = data[index].roleName;
      this.$confirm(
        "此操作将永久删除ID为【" +
          id +
          "】，名称为【" +
          name +
          "】的项, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          showCancelButton: true,
          type: "warning",
        }
      )
        .then(() => {
          data.splice(index, 1);
          this.categorys = data;
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          // 删除数据库中的对应项
          this.$http.post("role/delete", { id: id }).then((response) => {
            this.getRoles();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    add() {
      var powerListArray = this.addRole.powerList;
      var powerlist = [];
      powerListArray.forEach((element) => {
        powerlist.push(element[0] + "-" + element[1]);
      });
      this.addRole.powerList = powerlist;
      this.addRole.powerListOptions = powerListArray;
      this.$http.post("role/add", this.addRole).then((response) => {
        if (response.data.code != 200) {
          this.$message.error(response.data.message);
        } else {
          this.$message.success("添加成功");
          this.getRoles();
        }
      });
      this.addDialogFlag = false;
    },
    editrole(row) {
      this.editRole = JSON.parse(JSON.stringify(row));
      this.editDialogFlag = true;
    },
    edit() {
      var powerListArray = this.editRole.powerListOptions;
      var powerlist = [];
      powerListArray.forEach((element) => {
        powerlist.push(element[0] + "-" + element[1]);
      });
      this.editRole.powerList = JSON.stringify(powerlist);
      this.editRole.powerListOptions = JSON.stringify(powerListArray);
      this.$http.post("role/modify", this.editRole).then((response) => {
        this.getRoles();
        this.$message.success("修改成功");
      });
      this.editDialogFlag = false;
      this.editRole = {};
    },
    cancelEdit() {
      this.editDialogFlag = false;
      this.editRole = {};
    },
    cancelAdd() {
      this.addDialogFlag = false;
      this.addRole = {};
    },
  },
};
</script>

<style scope>
.main {
  overflow: hidden;
}
.el-input__inner {
  padding: 0 !important;
}
.addButton {
  margin-top: 12px !important;
  float: right;
  right: 30px;
}
.el-dialog .el-input {
  margin-bottom: 5px;
  margin-top: 5px;
}
.el-dialog .title {
  font-weight: 600;
  font-size: 15px;
  color: #c5464a;
}
</style>
